const http = require("http")
const opn = require("opn")
const render = require("./render")
const readPictures = require("./pictures")
const { processRequest } = require("./static")
module.exports = function serve(dir, port) {
    http.createServer((req, res) => {
        if (req.url === "/preview") {
            const pictures = readPictures(dir)
            res.writeHead(200, { "Content-Type": "text/html" })
            res.end(render(pictures))
        } else {
            processRequest(req, res, dir)
        }
    }).listen(port)

    const URL = `http://127.0.0.1:${port}/preview`
    opn(URL)
    console.log("Serving files from:")
    console.log(dir)
    console.log(URL)
}
